package com.motu.monstercity.server.game.logic;

import com.motu.monstercity.server.game.commondata.secretary.SecretaryIdentity;
import com.motu.monstercity.server.game.commondata.secretary.SecretaryMain;
import com.motu.monstercity.server.game.commondata.secretary.SecretaryPotentialSkill;
import com.motu.vertx.module.utility.mysql.MysqlClient;
import com.motu.vertx.module.utility.mysql.MysqlTool;
import com.motu.vertx.module.utility.toolset.Tool;
import io.vertx.core.Future;
import io.vertx.core.Promise;
import io.vertx.core.json.JsonArray;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.ConcurrentHashMap;

public class SecretaryBaseData {
    private static Logger logger = LoggerFactory.getLogger(SecretaryBaseData.class);
    private static ConcurrentHashMap<Integer, SecretaryMain> secretaryMainMap = new ConcurrentHashMap<>(); // 秘书表
    private static ConcurrentHashMap<Integer, SecretaryPotentialSkill> secretaryPotentialSkillMap = new ConcurrentHashMap<>(); // 秘书技能表
    private static List<Integer> potenialSkillList = new ArrayList<>();

    public static void init(List<Future> futures) {
        futures.add(initSecretaryMain());
        futures.add(initSecretaryPotentialSkill());
        futures.add(initSecretaryIdentity());
    }

    /**
     * 初始化秘书表
     */
    public static Future<Void> initSecretaryMain() {
        String table = "secretary_main";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, SecretaryMain> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        SecretaryMain entity = new SecretaryMain(result.getJsonObject(i));
                        tmp.put(entity.getSecretaryId(), entity);
                    }
                    secretaryMainMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static SecretaryMain getSecretaryMain(int id) {
        SecretaryMain result = secretaryMainMap.get(id);
        if (result == null) {
            logger.error("getSecretaryMain error key = " + id);
        }
        return result;
    }

    public static ConcurrentHashMap<Integer, SecretaryMain> getSecretaryMainMap() {
        return secretaryMainMap;
    }


    /**
     * 初始化秘书技能表
     */
    public static Future<Void> initSecretaryPotentialSkill() {
        String table = "secretary_potential_skill";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + " order by need_favorite desc;";// 按解锁的需求值从大到小排序
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, SecretaryPotentialSkill> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        SecretaryPotentialSkill entity = new SecretaryPotentialSkill(result.getJsonObject(i));
                        tmp.put(entity.getId(), entity);
                        potenialSkillList.add(entity.getId());
                    }
                    secretaryPotentialSkillMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static SecretaryPotentialSkill getSecretaryPotentialSkill(int id) {
        SecretaryPotentialSkill result = secretaryPotentialSkillMap.get(id);
        if (result == null) {
            logger.error("getSecretaryPotentialSkill error key = " + id);
        }
        return result;
    }

    public static List<Integer> getPotenialSkillList() {
        return potenialSkillList;
    }

    public static ConcurrentHashMap<Integer, SecretaryPotentialSkill> getSecretaryPotentialSkillMap() { return secretaryPotentialSkillMap; }

    private static ConcurrentHashMap<Integer, SecretaryIdentity> secretaryIdentityMap = new ConcurrentHashMap<>(); // 秘书基因表

    /**
     * 初始化秘书基因表
     */
    public static Future<Void> initSecretaryIdentity() {
        String table = "secretary_identity";
        Promise promise = Promise.promise();
        String sql = "select * from " + table + ";";
        MysqlTool.queryJsonArrayFromDb(MysqlClient.getCommonPool(), sql, new JsonArray(), res -> {
            if (res.succeeded()) {
                try {
                    ConcurrentHashMap<Integer, SecretaryIdentity> tmp = new ConcurrentHashMap<>();
                    JsonArray result = res.result();
                    logger.info("init " + table + " size:" + result.size());
                    for (int i = 0; i < result.size(); i++) {
                        SecretaryIdentity entity = new SecretaryIdentity(result.getJsonObject(i));
                        tmp.put(entity.getLevel(), entity);
                    }
                    secretaryIdentityMap = tmp;
                    promise.complete();
                } catch (Exception e) {
                    logger.error("init " + table + " error exception:" + Tool.getException(e));
                    promise.fail(e);
                }
            } else {
                logger.error(res.cause().getMessage());
                promise.fail(res.cause());
            }
        });
        return promise.future();
    }

    public static SecretaryIdentity getSecretaryIdentity(int level) {
        SecretaryIdentity result = secretaryIdentityMap.get(level);
        if (result == null) {
            logger.error("getSecretaryIdentity error key = " + level);
        }
        return result;
    }
    public static ConcurrentHashMap<Integer, SecretaryIdentity> getSecretaryIdentityMap() { return secretaryIdentityMap; }

}
